local skynet = require "skynet"
local tabletool = require "tabletool"
local msghelper = require "tablehelper"
local filelog = require "filelog"
local timer = require "timer"
local timetool = require "timetool"
require "pdkenum"

local RoomSeatLogic = {}

function RoomSeatLogic.init(seatobj, index)
	seatobj.index = index
	seatobj.state = ESeatState.SEAT_STATE_NO_PLAYER
	seatobj.is_tuoguan = EBOOL.FALSE
	return true
end

function RoomSeatLogic.clear(seatobj)
	seatobj.rid = 0
	seatobj.state = 0  --改坐位玩家状态
	seatobj.playerinfo = {}
end

function RoomSeatLogic.is_empty(seatobj)
	return (seatobj.state == ESeatState.SEAT_STATE_NO_PLAYER)
end

function RoomSeatLogic.dealcards(seatobj, turns)
	seatobj.handcards = tabletool.deepcopy(seatobj.cards)
	local noticemsg = {
		rid = seatobj.rid,
		seat_index = seatobj.index,
		cards = tabletool.deepcopy(seatobj.cards),
		turns = turns,
	}

	msghelper:sendmsg_to_tableplayer(seatobj, "cardinfoN",noticemsg)

	noticemsg.cards = {}
	msghelper:sendmsg_to_allwaitplayer("cardinfoN", noticemsg)
end

function RoomSeatLogic.dealtuoguan(gameobj,seat)
	local server = msghelper.get_server()
	if seat.is_tuoguan == EBOOL.FALSE then
		seat.is_tuoguan = EBOOL.TRUE
		local noticemsg = {
			rid = seat.rid,
			seat_index = seat.index,
			action_type = EActionType.ACTION_TYPE_REQUEST_TUOGUAN,
			cards = {},
			call_times = seat.jdz_score,
		}
		msghelper:sendmsg_to_alltableplayer("doactionN", noticemsg)
		--[[出过得牌牌堆
		if gameobj.CardsHeaps and type(gameobj.CardsHeaps) == "table" then
			local roundheaps = gameobj.CardsHeaps[#gameobj.CardsHeaps]
			if roundheaps and #roundheaps > 0 then
				local playercardhelper = roundheaps[#roundheaps].cardHelper
				local status
				local cards
				local onelast = false
				for _, _seat in ipairs(server.table_data.seats) do
					if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
						if _seat.cards and #_seat.cards == 1 then
							onelast = true
						end
					end
				end
				status, cards = server.logic.ddzlogic.deal_last_handscard(seat.cards, ECardsPowerCardType, EActionType.ACTION_TYPE_CHUPAI, {}, 0, onelast)
				if status == true then
					if type(cards) == "table" and #cards == #seat.cards then
						seat.tuoguan_punish = 0
					else
						seat.tuoguan_punish = 1
					end
				elseif status == false then
					status, cards = server.logic.ddzlogic.deal_last_handscard(seat.cards, ECardsFollowCard, EActionType.ACTION_TYPE_FOLLOW_CHUPAI, playercardhelper, true, onelast)
					if status == true then
						if type(cards) == "table" and #cards == #seat.cards then
							seat.tuoguan_punish = 0
						else
							seat.tuoguan_punish = 1
						end
					else
						seat.tuoguan_punish = 1
					end
				end 
			else
				seat.tuoguan_punish = 1
			end
		else
			seat.tuoguan_punish = 1
		end
		--]]
		
		----如果已经注册玩家操作超时定时器,则取消
		if gameobj.action_seat_index == seat.index and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END_AFTER 
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_REAL_END and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END 
			and gameobj.state ~= ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END then
			if gameobj.timer_id and gameobj.timer_id > 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				timer.cleartimer(gameobj.timer_id)
				gameobj.timer_id = -1
			end
			local doactionntcmsg = {
				rid = gameobj.seats[gameobj.action_seat_index].rid,
				seat_index = gameobj.action_seat_index,
				action_type = gameobj.action_type,
				action_to_time = gameobj.action_to_time,
			}
			local lefttime = gameobj.action_to_time - timetool.get_time()
			if lefttime <= gameobj.conf.tuoguan_action_time and lefttime >= 0 then
				gameobj.timer_id = timer.settimer(lefttime*100, "doaction", doactionntcmsg)
			else
				gameobj.timer_id = timer.settimer(gameobj.conf.tuoguan_action_time*100, "doaction", doactionntcmsg)
			end
		end
	end
end

function RoomSeatLogic.canceltuoguan(gameobj,seat)
	local server = msghelper.get_server()
	if seat.is_tuoguan == EBOOL.TRUE then
		seat.timeout_count = 0
		seat.is_tuoguan = EBOOL.FALSE
		seat.tuoguan_punish = 0
		local noticemsg = {
			rid = seat.rid,
			seat_index = seat.index,
			action_type = EActionType.ACTION_TYPE_CANCEL_TUOGUAN,
			cards = {},
			call_times = seat.jdz_score,
		}
		msghelper:sendmsg_to_alltableplayer("doactionN", noticemsg)
		----如果已经注册玩家操作超时定时器,则取消,在结算阶段不取消定时器,防止误取消定时器结算阶段中断.
		if gameobj.action_seat_index == seat.index and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END_AFTER 
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_REAL_END and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END 
			and gameobj.state ~= ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END then
			if gameobj.timer_id and gameobj.timer_id > 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				timer.cleartimer(gameobj.timer_id)
				gameobj.timer_id = -1
			end
			local doactionntcmsg = {
				rid = gameobj.seats[gameobj.action_seat_index].rid,
				seat_index = gameobj.action_seat_index,
				action_type = gameobj.action_type,
				action_to_time = gameobj.action_to_time,
			}
			local lefttime = gameobj.action_to_time - timetool.get_time()
			if lefttime >= 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				gameobj.timer_id = timer.settimer(lefttime*100, "doaction", doactionntcmsg)
			end
		end
	end
end

function RoomSeatLogic.setSeatstate(gameobj)
	if gameobj.action_type == EActionType.ACTION_TYPE_CHUPAI then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_CHUPAI
	elseif gameobj.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_FOLLOW_CHUPAI
	elseif gameobj.action_type == EActionType.ACTION_TYPE_CHECK then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_CHECK
	end
end

function RoomSeatLogic.resetstate(seatobj)
	seatobj.state = ESeatState.SEAT_STATE_WAIT_READY
	seatobj.timeout_count = 0
	seatobj.is_tuoguan = EBOOL.FALSE
	seatobj.win = 0		---表示玩家胜利还是失败
	seatobj.is_ready = EBOOL.FALSE
	seatobj.ready_timer_id = -1 ---准备倒计时定时器
	seatobj.ready_to_time = 0   ---准备到期时间
	seatobj.cards = {}   ---玩家手牌
	seatobj.handcards = {} --初始手牌
	seatobj.multiple = 0		 ----玩家加倍倍数
	seatobj.balance_score = 0	 ----最终结算分数
	seatobj.tuoguan_punish = 0		-------
	seatobj.followhandsnum = 0
	seatobj.holdvalue = 0 		 ----玩家货币持有量
	seatobj.dealnum = 0
	seatobj.bomboff = nil
	seatobj.diamond_base = nil
	seatobj.standup = nil
	seatobj.start_diamond = nil
end

function RoomSeatLogic.clear_seat(seat)
	seat.rid = 0
	seat.state = ESeatState.SEAT_STATE_NO_PLAYER
	seat.playerinfo = {}
	seat.is_tuoguan = EBOOL.FALSE
	seat.timeout_count = 0
	seat.win = 0
	seat.diamond = 0
	seat.getdiamond = 0
	seat.fangka = 0
	seat.multiple = 0		----玩家加倍倍数
	seat.ready_to_time = 0
	seat.cards = nil
	seat.handcards = nil --初始手牌
	seat.is_disconnect = nil
	seat.followhandsnum = 0
	seat.holdvalue = 0		----玩家货币持有量
	seat.dealnum = 0
	seat.bomboff = nil
	seat.diamond_base = nil
	seat.standup = nil
	seat.start_diamond = nil
	seat.sametabledata = nil
	seat.rematched = nil
	if seat.ready_timer_id and seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
end

function RoomSeatLogic.onegamestart_initseat(seat)
	seat.state = ESeatState.SEAT_STATE_PLAYING
	seat.ready_to_time = 0
	if seat.ready_timer_id and seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
	seat.diamond_base = seat.getdiamond
end

function RoomSeatLogic.onegamestart_initseat2(seat)
	if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
		seat.state = ESeatState.SEAT_STATE_PLAYING
	end
	seat.ready_to_time = 0
	if seat.ready_timer_id and seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
	seat.diamond_base = seat.getdiamond
end

return RoomSeatLogic